home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 4 / Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso / Development / Source / Telnet 2.6.1d1 4⁄26⁄94 Folder / source / main / popup.c < prev    next >
Text File  |  1994-04-18  |  4KB  |  195 lines

  1. /*
  2. *    popup.c
  3. *    Code for doing popup menus in our dialog boxes
  4. *    Credit for this goes to Apple Sample Code and a conglomeration of other code I've seen.
  5. *
  6. *****************************************************************
  7. *    NCSA Telnet for the Macintosh                                *
  8. *                                                                *
  9. *    National Center for Supercomputing Applications                *
  10. *    Software Development Group                                    *
  11. *    152 Computing Applications Building                            *
  12. *    605 E. Springfield Ave.                                        *
  13. *    Champaign, IL  61820                                        *
  14. *                                                                *
  15. *    Copyright (c) 1986-1992,                                    *
  16. *    Board of Trustees of the University of Illinois                *
  17. *****************************************************************
  18. *  Revisions:
  19. *  8/92        Telnet 2.6:    Initial version.  Jim Browne
  20. */
  21.  
  22. #ifdef MPW
  23. #pragma segment Configure
  24. #endif
  25.  
  26. #include "TelnetHeader.h"
  27. #include "popup.h"
  28. #include "popup.proto.h"
  29.  
  30. static popup *FindPopup(short item);
  31. static pascal void DrawPopUp(DialogPtr dptr, short item);
  32. PROTO_UPP(DrawPopUp, UserItem);
  33.  
  34. popup *Popup;    /* Current popup structure array */
  35.  
  36. static popup *FindPopup( short item)
  37. {
  38.     popup *p = Popup;
  39.  
  40.     while (p->item) {
  41.             if (p->item == item)
  42.                 return p;
  43.             ++p;
  44.     }
  45.     return 0;                    /* Not found */
  46. }
  47.  
  48. SIMPLE_UPP(DrawPopUp, UserItem);
  49. static pascal void DrawPopUp(DialogPtr dptr, short item)
  50. {
  51.     char text[256];
  52.     char* s;
  53.     PenState savePen;
  54.     Rect box;
  55.     FontInfo info;
  56.     short type, width, newwidth;
  57.     Handle hdl;
  58.     popup *p;
  59.  
  60.     GetPenState(&savePen);
  61.     GetFontInfo(&info);
  62.  
  63.     GetDItem(dptr, item, &type, &hdl, &box);
  64.  
  65.     /* Get and draw menu title. Get item text. */
  66.     p = FindPopup(item);
  67.     if (!p || !p->h) return;
  68.     s = (char *)(**(p->h)).menuData;    /* Menu title */
  69.     MoveTo(box.left - StringWidth((StringPtr)s) - 2, box.top + info.ascent);
  70.     DrawString((StringPtr)s);
  71.     GetItem(p->h, p->choice, (StringPtr)text);
  72.  
  73.     width = (box.right - box.left) - (CharWidth(checkMark) + 2);
  74.     newwidth = StringWidth((StringPtr)text);
  75.  
  76.     if (newwidth > width) {
  77.         width -= CharWidth('…');
  78.         do {
  79.             newwidth -= CharWidth(text[text[0]]);
  80.             --text[0];
  81.             } while (newwidth > width && text[0]);
  82.         ++text[0];
  83.         text[text[0]] = '…';
  84.         }
  85.     
  86.     MoveTo(box.left + CharWidth(checkMark) + 2, box.top + info.ascent);
  87.     DrawString((StringPtr)text);
  88.  
  89.     InsetRect(&box, -1, -1);                    /* A la Apple Sample Code */
  90.     PenSize(1, 1);
  91.     FrameRect(&box);
  92.     MoveTo(box.left + 2, box.bottom);
  93.     LineTo(box.right, box.bottom);
  94.     LineTo(box.right, box.top + 2);
  95.  
  96.     SetPenState(&savePen);
  97. }
  98.  
  99. Boolean PopupMousedown(    DialogPtr dptr, EventRecord *event, short *i)
  100. {
  101.     short item, choice, type;
  102.     popup *p;
  103.     Point clikloc;
  104.     long chosen;
  105.     Handle hdl;
  106.     Rect box, title;
  107.     Boolean result = FALSE;
  108.  
  109.     clikloc = event->where;
  110.     GlobalToLocal(&clikloc);
  111.     if ((item = FindDItem(dptr, clikloc) + 1) < 1)
  112.         return result;
  113.     p = FindPopup(item);
  114.     if (!p || !p->h)
  115.         return result;
  116.  
  117.     GetDItem(dptr, item, &type, &hdl, &box);
  118.     clikloc = topLeft(box);
  119.     LocalToGlobal(&clikloc);
  120.     title = box;
  121.     title.right = box.left - 1;
  122.     title.left = title.right - StringWidth((**(p->h)).menuData) - 2;
  123.     box.top -= 1;
  124.     box.left -= 1;
  125.     box.bottom += 2;
  126.     box.right += 2;
  127.  
  128.     EraseRect(&box);
  129.     InvertRect(&title);
  130.  
  131.     InsertMenu(p->h, hierMenu);
  132.     SetItemMark(p->h, p->choice, checkMark);
  133.     CalcMenuSize(p->h);
  134.     chosen = PopUpMenuSelect(p->h, clikloc.v, clikloc.h, p->choice);
  135.     SetItemMark(p->h, p->choice, noMark);
  136.     DeleteMenu((**(p->h)).menuID);
  137.  
  138.     if (chosen) {
  139.         choice = chosen & 0xFFFF;            /* Apple sez ignore high word */
  140.         if (choice != p->choice) {
  141.             p->choice = choice;
  142.             *i = item;
  143.             result = TRUE;
  144.         }
  145.     }
  146.  
  147.     InvertRect(&title);
  148.     DrawPopUp(dptr, item);
  149.  
  150.     return result;
  151. }
  152.  
  153. void PopupInit( DialogPtr dptr, popup *popups)
  154. {
  155.     popup *p;
  156.     short w;
  157.     FontInfo info;
  158.     short type;
  159.     Handle hdl;
  160.     Rect box;
  161.  
  162.     Popup = p = popups;
  163.     SetPort(dptr);
  164.     GetFontInfo(&info);
  165.     while (p->item) {
  166.         if (p->h) {
  167.             /* Correct user item box. */
  168.             GetDItem(dptr, p->item, &type, &hdl, &box);
  169.             CalcMenuSize(p->h);
  170.             w = (**(p->h)).menuWidth;
  171.             w += 14;
  172.             if (w < (box.right - box.left))
  173.                 box.right = box.left + w;
  174.             box.bottom = box.top + info.ascent + info.descent + info.leading;
  175.             SetDItem(dptr, p->item, type, (Handle)DrawPopUpUPP, &box);
  176.         }
  177.         ++p;
  178.     }
  179. }
  180.  
  181. void PopupCleanup(void)
  182. {
  183.     popup *p = Popup;
  184.  
  185.     while (p->item) {
  186.         if (p->h)
  187.             DisposeMenu(/*(Handle)*/p->h);    // Since we're using NewMenu's, we need
  188.                                             // to do a DisposeMenu, rather than a 
  189.                                             // Releaseresource
  190.         ++p;
  191.     }
  192. }
  193.  
  194.  
  195.